 /*
 *  forms.js
 *
 *  A collection of functions that format the content
 * within the HTML forms in index.html.
 *
 *  @author Benjamin Co
 */


var cs441GoogleMapsViz = cs441GoogleMapsViz || {};

/*
 *  handleFocus()
 * If the newly focused on HTML element 'element' contains 
 * the original, default suggestion value, then empty 
 * the field.
 *
 * @param element The HTML element under inspection
 * @return void
 */
function handleFocus(element)
{
    if (element.value == element.defaultValue) 
    {
    	element.style.color = '#333';
        element.value = '';
    }
}

/*
 *  hanldeBlur()
 * If the newly focused on HTML element 'element' contains 
 * nothing, replace it wih the original suggestion text.
 *
 * @param element The HTML element under inspection
 * @return void
 */
function handleBlur(element)
{
    if (element.value == '')
    {
    	element.style.color = '#999';
        element.value = element.defaultValue;
    }
}

function toggle_visibility(visible, hidden) 
{
	var currentVis = document.getElementById(visible);
	var alt = document.getElementById(hidden);
	if(currentVis.style.display != 'none')
	{
		currentVis.style.display = 'none';
		alt.style.display = "inline-block";
	}
	else 
	{
		currentVis.style.display = 'inline-block';
		alt.style.display = "none";
	}
}

cs441GoogleMapsViz.collegeMajors = {
    0  : "Undecided",
    900  : "Engineering - Undeclared",
    908  : "Civil Egr:Civil Track",
    907  : "Civil Egr:Environmental Track",
    909  : "Elec Egr:Electrical Track",
    910  : "Mechanical Engineering",
    500  : "Business Admin:Undeclared",
    913  : "Engineering Management",
    925  : "Elec Egr:Computer Track",
    701  : "Computer Science",
    802  : "Elementary Education",
    803  : "Secondary Education",
    502  : "Accounting",
    509  : "Marketing",
    515  : "Finance",
    517  : "Economics",
    514  : "Global Business",
    507  : "Entrepreneurship",
    513  : "Marketing and Management",
    508  : "Operations Technology Mgmt",
    2208  : "Sociology",
    2301  : "Theology",
    2207  : "Political Science",
    1201  : "Nursing",
    1999  : "Life Science",
    2001  : "Psychology",
    1902  : "Physics",
    1701  : "Mathematics",
    1509  : "Philosophy",
    1005  : "Music",
    2205  : "History",
    0325  : "German Studies",
    0324  : "French Studies",
    0301  : "Environmental Science",
    0302  : "Envir Ethics & Policy",
    1007  : "Drama",
    1501  : "English",
    0600  : "Communication",
    0601  : "Communication",
    1906  : "Chemistry: ACS",
    1907  : "Chemistry: Biochemistry",
    1905  : "Chemistry",
    0401  : "Biology"
}

var majorOptions = [
    "Undecided#0000",
    "Engineering - Undeclared#0900",
    "Civil Egr:Civil Track#0908",
    "Civil Egr:Environmental Track#0907",
    "Elec Egr:Electrical Track#0909",
    "Mechanical Engineering#0910",
    "Business Admin:Undeclared#0500",
    "Engineering Management#0913",
    "Elec Egr:Computer Track#0925",
    "Computer Science#0701",
    "Elementary Education#0802",
    "Secondary Education#0803",
    "Accounting#0502",
    "Marketing#0509",
    "Finance#0515",
    "Economics#0517",
    "Global Business#0514",
    "Entrepreneurship#0507",
    "Marketing and Management#0513",
    "Operations Technology Mgmt#0508",
    "Sociology#2208",
    "Theology#2301",
    "Political Science#2207",
    "Nursing#1201",
    "Life Science#1999",
    "Psychology#2001",
    "Physics#1902",
    "Mathematics#1701",
    "Philosophy#1509",
    "Music#1005",
    "History#2205",
    "German Studies#0325",
    "French Studies#0324",
    "Environmental Science#0301",
    "Envir Ethics & Policy#0302",
    "Drama#1007",
    "English#1501",
    "Communication#0600",
    "Communication#0601",
    "Chemistry: ACS#1906",
    "Chemistry: Biochemistry#1907",
    "Chemistry#1905",
    "Biology#0401"
];


function populateMajorOptions()
{
    // Every major offerd at University of Portland
    // Each major has a name (before '#')
    // Each major also has a Hardwick-Day number assignment (after '#')
    // Here, we include both, delimitted by a #
    majorOptions.sort();
    /*$.each(majorOptions ,function(i,value) {   
        var major = value.split('#');
	$('#majorOptions')
            .append($('<li class="majors"></li>')
		    .attr("value",major[1])
		    .text(major[0]));
    });*/
    $.each(majorOptions ,function(i,value) {   
        var major = value.split('#');
	$('#majorOptions')
            .append($('<li class="majors"></li>')
		    .attr("value",major[1])
		    .text(major[0]));
    });
}


// This function is applied when the document is loaded
$(document).ready(function()
{	
    populateMajorOptions(); // populates the multiple select box
    
    $("input[name$='limit']").click(function(){
	
	var radio_value = $(this).val();
	
	if(radio_value =='zip') {
	    $("#zipLabel").slideDown();
	    $("#stateLabel").hide();
	}
	else if(radio_value == 'state') {
	    $("#stateLabel").slideDown();
	    $("#zipLabel").hide();
	}
    });
    $("#majorOptions").bind("mousedown", function(e) {
	e.metaKey = true;
    }).selectable({
	selecting: function (event, ul) {
	    if(ul.selecting.id == "all"){
		$(".majors", this).each(function() {
		    $(this).removeClass('ui-selected');
		});
	    }
	    else {
		$('#all').removeClass('ui-selected');
	    }
	}
    });    

    $('#reset').click(function() {	
	$(".ui-selected").each(function() {
		    $(this).removeClass('ui-selected');
	});
	$('#all').addClass('ui-selected');
    });

    $('#all').addClass('ui-selected');
    // hide the state by default
    $("#stateLabel").hide();    
});
